package beans;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import manager.SessionManager;

import structures.xmltable.ServiceTableColumn;
import util.Size;

@ManagedBean(name="sizeBean")
@RequestScoped
public class ScreenSizeManagerBean {

	public static final Size NARROW_SCREEN = new Size("1024", "768");
	public static final Size LARGE_SCREEN = new Size("1920", "933");
	
	private Integer height;
	private Integer width;
	
	public Integer getHeight(){
		if(height == null){
			String heightObj = null;
			heightObj = SessionManager.getHeight();
			if(heightObj != null)
				height = new Integer(heightObj.toString());
			else
				height = new Integer(LARGE_SCREEN.getHeight());
		}
		return height;
	}
	
	public Integer getWidth(){
		if(width == null){
			String widthObj = null;
			widthObj = SessionManager.getWidth();
			if(widthObj != null)
				width = new Integer(widthObj.toString());
			else
				width = new Integer(LARGE_SCREEN.getWidth());
		}
		return width;
	}

	public void setHeight(Integer height) {
		SessionManager.setHeight(height.toString());
		this.height = height;
	}

	public void setWidth(Integer width) {
		SessionManager.setWidth(width.toString());
		this.width = width;
	}

	public Integer getTableScrollHeight(){
		Integer height = getHeight();
		return 
				!isWideScreen() ? 
//					(height - 346) /2
					(height - 247) - 20 
							: 
					(height - 240) - 2
						;
	}
	public Integer getInTableScrollHeight(){
		return getTableScrollHeight() - 94;
	}
	public String getMiddleHeight(){
		Integer height = getHeight();
		height = height - 105 - 31 - 8;
		return height.toString();
	}
	public String getMiddleWidth(){
		Integer width = getWidth();
		width = width /2 - 3;
		return width.toString();
	}
	public Integer getPaneSize(){
		Integer size = getWidth()/2 - 550;
		if(size < 260)
			size = -1;
		return size;
	}
	public String getPaneSizeStr(){
		String size = isWideScreen() ? getPaneSize() + "" : "50%";
		return size;
	}

	public Integer getFullScreenTableWidth(){
		Integer width = getWidth();
		return (width - 20);
	}
	
	public Integer getFullScreenTableHeight(){
		Integer height = getHeight();
		return (height - 98);
	}
	
	public Integer getPaneTextLength(){
//		return isWideScreen() ? (getPaneSize()-65) / 8 + 7 : 100;
		return isWideScreen() ? (getPaneSize()-65) / 8 + 10 : 100;
	}
	
	public String getSessionWidth(){
		return null;
	}
	public void setSessionWidth(String width){
		setWidth(new Integer(width));
	}
	public String getInPanePosition() {
		return isWideScreen() ? "west" : "south";
	}
	public String getOutPanePosition() {
		return isWideScreen() ? "east" : "south";
	}
	public Integer compareColumnWidth(int columnCount) {
		Integer width = getFullScreenTableWidth();
		Integer response = null;
		if(200 * columnCount > width)
			response = width / columnCount;
		else
			response = 200;
		System.out.print("\n-------------------------------\n");
		System.out.print(response);
		return response;
	}

	public String getSessionHeight() {
		return null;
	}

	public void setSessionHeight(String height) {
		setHeight(new Integer(height));
	}
	public boolean isWideScreen(){
		Integer size = getWidth()/2 - 550;
		return size > 260;
	}
	public boolean isWideScreenTwoThirds(){
		Integer size = getWidth()/2 - 550;
		return size > 260 - 230;
	}
	public String tableWidthByColumns(List<ServiceTableColumn> tableColumns){
		Integer sonuc = 60;
		for (ServiceTableColumn serviceTableColumn : tableColumns) {
			sonuc += serviceTableColumn.getSize();
		}
		return sonuc.toString();
	}
}
